<?PHP
require_once('../output.php');
require('usermanager.class.php');

$action = $_POST['action'];

$output = Output::getInstance();
$um = UserManager::getInstance();

switch($action)
{
	case 'unamecheck':
		$username = $_GET['username'];
		if(isset($username))
		{
			$output->register('available',$um->checkUsername($username));
		}
		else
		{
			$output->invalidate('No Username');
		}
	break;
	case 'newuser':
		$username = $_POST['username'];
		$password = $_POST['password'];
		$password_v = $_POST['password-v'];
		$email = $_POST['email'];
		$email_v = $_POST['email-v'];

		if($password === $password_v)
		{
			if($email === $email_v)
			{
				$res = $um->createUser($username,$password,$email);
				
				if($res == UserManager::REQUEST_SUCCESS)
				{
					$output->register('username',$username);
					$output->register('email',$email);
				}
				else
				{
					switch($res)
					{
						case UserManager::USERNAME_FAILURE: $output->invalidate('Username invalid'); break;
						case UserManager::PASSWORD_FAILURE: $output->invalidate('Password invalid'); break;
						case UserManager::EMAIL_FAILURE:	$output->invalidate('Password invalid'); break;
					}
				}
			}
			else
			{
				$output->invalidate('Emails do not match');
			}
		}
		else
		{
			$output->invalidate('Passwords do not match');
		}
	break;
	case 'login':
		$uname = $_POST['username'];
		$pass = $_POST['password'];
		
		$user = $um->getUser($uname, $pass);
		$output = Output::getInstance();
		if($user == false)
		{
			//Failed login
			$output->invalidate('Incorrect username/pass');
		}
		else
		{
			//Login okay
		}
	break;
}

$output->send();
?>